package com.tao.dao.group.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.tao.dao.SupeDao;
import com.tao.dao.group.GroupDao;
import com.tao.domain.Group;

@Repository
public class GroupDaoImpl extends SupeDao implements GroupDao {
     

	@SuppressWarnings("unchecked")
	@Override
	public List<Group> findAll(Group t) {
		Session session  = (Session) sessionFactory.openSession();
		
		String hql = "from   Group  where  1=1 ";
		if(null!=t&&!t.equals("")){
		if(null!= t.getUin()&&!t.getUin().equals("")){
			hql = hql +" and uin = :uin";
		}
		if(null!= t.getGroupName()&&!t.getGroupName().equals("")){
			hql = hql +" and groupName = :username";
		}
		if(null!= t.getGroupNickName()&&!t.getGroupNickName().equals("")){
			hql = hql +" and groupNickName = :nickname";
		}
	}
		
		  Query q = session.createQuery(hql);
		  if(null!=t&&!t.equals("")){
          q.setString("uin", t.getUin());
          q.setString("username", t.getGroupName());
          q.setString("nickname", t.getGroupNickName());
		  }
          List<Group> list = q.list();
          System.out.println("session"+hql);
		  session.close();
		  System.out.println("session_end");
        		 return list;
	}

	@Override
	public void insert(Group t) {
		Session session  = (Session) sessionFactory.openSession();
		session.save(t);
		session.close();
	}

	@Override
	public void update(Group t) {
		Session session  = (Session) sessionFactory.openSession();
		session.update(t);
		session.close();
	}



	@Override
	public void delete(Group t) {
		Session session = null;  
		Transaction tx = null;  
		
		session = sessionFactory.openSession();  
		tx = session.beginTransaction();
		
		String hql = "delete from Group  where  1=1 ";
		if(null!=t&&!t.equals("")){
			if(null!= t.getUin()&&!t.getUin().equals("")){
				hql = hql +" and uin = :uin";
			}
			if(null!= t.getGroupName()&&!t.getGroupName().equals("")){
				hql = hql +" and groupName = :username";
			}
			if(null!= t.getGroupNickName()&&!t.getGroupNickName().equals("")){
				hql = hql +" and groupNickName = :nickname";
			}
		}
		  Query q = session.createQuery(hql);
         
          if(null!=t&&!t.equals("")){
        	  
        	  if(null!= t.getUin()&&!t.getUin().equals("")){
        		  q.setString("uin", t.getUin());
      		}
      		if(null!= t.getGroupName()&&!t.getGroupName().equals("")){
      			 q.setString("UserName", t.getGroupName());
      		}
      		if(null!= t.getGroupNickName()&&!t.getGroupNickName().equals("")){
      			q.setString("NickName", t.getGroupNickName());
      		}  
         
          }
         q.executeUpdate();  
		tx.commit();  
		 System.out.println("session"+hql);
		  session.close();
		  System.out.println("session_end");
		 
		
	}

}
